﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.SharePoint;


namespace SqlRestCtrl
{
	public class SqlData : AzureConnect.WcfConfig
	{
		//this method must be overridden so the proxy can be 
		//configured and created correctly
		public override bool ExecuteRequest()
		{
			try
			{
				//create the proxy instance with bindings and endpoint the base class
				//configuration control has created for this
				AzProducts.ProductsClient cust =
					new AzProducts.ProductsClient(this.FedBinding,
					 this.WcfEndpointAddress);

				//configure the channel so we can call it with 
				//FederatedClientCredentials. 
				SPChannelFactoryOperations.ConfigureCredentials<AzProducts.IProducts>
					 (cust.ChannelFactory,
					 Microsoft.SharePoint.SPServiceAuthenticationMode.Claims);

				//create a channel to the WCF endpoint using the 
				//token and claims of the current user
				AzProducts.IProducts claimsWCF =
					SPChannelFactoryOperations.CreateChannelActingAsLoggedOnUser
					<AzProducts.IProducts>(cust.ChannelFactory,
					 this.WcfEndpointAddress,
					new Uri(this.WcfEndpointAddress.Uri.AbsoluteUri));

				//set the client property for the base class
				this.WcfClientProxy = claimsWCF;
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.Message);
			}

			//now that the configuration is complete, call the method
			return base.ExecuteRequest();
		}
	}
}
